*** Final data set
*** 1 March 2023

*** working directory
global path "" // Define path here

cd "${path}State file"






*** 1. Aggregate city data to state-level ***
*-------------------------------------------*

*** Dincecco and Onorato (2016) data
* load data
use "${path}Raw data/Dincecco and Onorato 2016.dta", clear

* aggregate by state and year
collapse (mean) DO_dummy_all150 DO_commune, by(state year)

* rename variables
rename DO_dummy_all150 DO_dummy_all150_share
rename DO_commune DO_commune_mean
rename state Name

* recode missings to zero
replace DO_dummy_all150_share = 0 if DO_dummy_all150_share == .

* drop places with missing state
drop if Name == ""

* save
saveold "${path}State file/DO_state.dta", replace version(12)


*** protestantism data (Rubin)
* load data
use "${path}Raw data/protestantismRubin_long.dta", clear

* restrict to state non-missing
keep if state != ""

* collapse
collapse (mean) Rubin_prot, by(state year)

* rename state
rename state Name

* save
saveold "${path}State file/protestantismRubin_state.dta", replace version(12)



*** 2. Prepare final data ***
*---------------------------*

*** load and prepare empty data frame
* load
use "${path}State file/StateDataFrame.dta", clear
sort name year

* drop repeat observations
bysort name year: gen count = _n
drop if count != 1
drop count

* rename name variable
rename name Name

* destring year
destring year, replace


*** merging
* file "AssignmentToState.dta"
merge 1:1 Name year using "${path}State file/AssignmentToState.dta", nogen

* Dincecco and Onorato 2016 data
merge 1:1 Name year using "${path}State file/DO_state.dta", nogen

* Primogeniture
merge 1:1 Name year using "${path}Raw data/primogenitureKS.dta"
drop if _m==2
drop _m

* papal conflict
merge 1:1 Name year using "${path}Raw data/papalconflictBrecke.dta"
drop if _m==2
drop _m
replace conflicts_RA_forward_papal = 0 if conflicts_RA_forward_papal == .

* fragmentation
merge 1:1 Name year using "${path}State file/fragmentation.dta", nogen

* Rubin protestantism
merge 1:1 Name year using "${path}State file/protestantismRubin_state.dta" 
drop if _m==2
drop _m

bysort Name: gen aux = Rubin_prot if year == 1530
bysort Name: egen Rubin_prot1530 = mean(aux)
drop aux
bysort Name: gen aux = Rubin_prot if year == 1560
bysort Name: egen Rubin_prot1560 = mean(aux)
drop aux
bysort Name: gen aux = Rubin_prot if year == 1600
bysort Name: egen Rubin_prot1600 = mean(aux)
drop aux

replace Rubin_prot = 0 if year < 1530 & (Rubin_prot1530 != . | Rubin_prot1560 != . | Rubin_prot1600 != .)
replace Rubin_prot = Rubin_prot1530 if year > 1530 & year < 1560
replace Rubin_prot = Rubin_prot1560 if year > 1560 & year < 1600
replace Rubin_prot = Rubin_prot1600 if year > 1600 & year != .
drop Rubin_prot1530 Rubin_prot1560 Rubin_prot1600



*** save
saveold "StateDataFinal.dta", replace version(12)













